package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.ScenicSpot;
import org.wmq.travelagency02.service.ScenicSpotService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/scenic-spot")
@RequiredArgsConstructor
public class ScenicSpotController {
    private final ScenicSpotService scenicSpotService;


    @GetMapping("/{id}")
    public Result<ScenicSpot> getById(@PathVariable Long id){
        ScenicSpot result = this.scenicSpotService.getById(id);
        if (result == null){
            return Result.error("查询失败");
        }
        return Result.ok(result);
    }

    @PostMapping
    public Result<Void> save(@RequestBody ScenicSpot scenicSpot){
        scenicSpot.setCreateTime(LocalDateTime.now());
        scenicSpot.setUpdateTime(LocalDateTime.now());
        if (scenicSpotService.save(scenicSpot)){
            return Result.ok();
        }
        return  Result.error("添加失败");
    }

    @PutMapping
    public Result<Void> update(@RequestBody ScenicSpot scenicSpot){
        scenicSpot.setUpdateTime(LocalDateTime.now());
        if (scenicSpotService.updateById(scenicSpot)){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id){
        if (scenicSpotService.removeById(id)){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
